/*
  VoIP Wholesale 1.0
  ------------------
  Document   : module_functions.js
  Created on : Sep 14, 2011, 4:16:45 PM
  Author     : Milos Jovanovic <miloscode@gmail.com>
  Description:

 */
 
function simulate()
{
    ajaxURL = AJAX_URL + '/simulate/';
	
	$.ajax({
			type: "POST",
			url: ajaxURL,
			data: $('#simulator-form').serialize(),
			async: true,
			success: function(response){

				var parsed = response.split('|');
				
				
				if(parsed[0] == "true")
				{
									html = "";
									
                                    html += "<p>";                                
                                    html += parsed[1] +"</p>";
									
                                    $('#response')
                                        .removeClass('ui-state-error ui-corner-all')									
                                        .html(html);                                
									
				
				}
				else if(parsed[0] == "false")
				{							
									html = "";				
									
									html += "<p><span class='highlight-icon ui-icon ui-icon-alert'></span>";
									
                                    html += parsed[1] +"</p>";
									
									$('#destination-number')
                                        .addClass('ui-state-error ui-corner-all');
										
                                    $('#response')
                                        .addClass('ui-state-error ui-corner-all')
                                        .html(html);                                
				}
	
				else
				{
									$('#response')
                                        .addClass('ui-state-highlight ui-corner-all')
                                        .html(response);                                
				}
			}
			});
}
 
function load_country_routes_external(route_name)
{
	var country_id = $('#country').val();
	
	$('#route_name').html('<option value="">'+lang_term_route_name+'</option>');
	
    ajaxURL = AJAX_URL + '/load_country_routes_external/' + country_id + '/' + route_name;	
	
	$.ajax({
			type: "POST",
			url: ajaxURL,
			async: true,
			success: function(response){
				if(response != "false")
				{	
					$('#route_name').html(response);
				}
			}
			});
} 

function load_country_routes_internal(route_name)
{
	var country_id = $('#country').val();
	
	$('#route_name').html('<option value="">'+lang_term_route_name+'</option>');
	
    ajaxURL = AJAX_URL + '/load_country_routes_internal/' + country_id + '/' + route_name;	
	
	$.ajax({
			type: "POST",
			url: ajaxURL,
			async: true,
			success: function(response){
				if(response != "false")
				{	
					$('#route_name').html(response);
				}
			}
			});
} 


function load_country_routes_special(route_name)
{
	var country_id = $('#country').val();
	
	$('#route_name').html('<option value="">'+lang_term_route_name+'</option>');
	
    ajaxURL = AJAX_URL + '/load_country_routes_special/' + country_id + '/' + route_name;	
	
	$.ajax({
			type: "POST",
			url: ajaxURL,
			async: true,
			success: function(response){
				if(response != "false")
				{	
					$('#route_name').html(response);
				}
			}
			});
} 

 
function save_settings()
{
        ajaxURL = AJAX_URL + '/save_settings/';
            
        //alert(ajaxURL);

	$.ajax({
			type: "POST",
			url: ajaxURL,
			data: $('#module-settings-form').serialize(),
			async: true,
			success: function(response){

                        reset_error_state();
			
                        var parsed = response.split('|');
                        
                        var html = '';
                        
                                    html += "<p><span class='highlight-icon ui-icon ui-icon-info'></span>";                                
                                    html += parsed[1] +"</p>";
                                
                                    $('#response')
                                        .addClass('ui-state-highlight ui-corner-all')
                                        .html(html);                                
			}
			});


} 
 
function save_new_route()
{
        ajaxURL = AJAX_URL + '/add_new_route/' + numberingPlan + '/';
            
        //alert(ajaxURL);

	$.ajax({
			type: "POST",
			url: ajaxURL,
			data: $('#new-route').serialize(),
			async: true,
			success: function(response){

                        reset_error_state();
			
                        var parsed = response.split('|');
                        
                        var html = '';
                        
                        if(parsed[0] == "true")
                            {
                                    html += "<p><span class='highlight-icon ui-icon ui-icon-info'></span>";                                
                                    html += parsed[1] +"</p>";
                                
                                    $('#response')
                                        .addClass('ui-state-highlight ui-corner-all')
                                        .html(html);                                
                            }

                       else if(parsed[0] == "empty_route_name")
                            {
                                    html += "<p><span class='highlight-icon ui-icon ui-icon-alert'></span>";                                
                                    html += parsed[1] +"</p>";
                                
                                    $('#response')
                                        .addClass('ui-state-error ui-corner-all')
                                        .html(html);       
                                        
                                    $('#route-name')
                                        .addClass('ui-state-error');
                            }

                        else if(parsed[0] == "empty_dialcodes")
                            {
                                    html += "<p><span class='highlight-icon ui-icon ui-icon-alert'></span>";                                
                                    html += parsed[1] +"</p>";
                                
                                    $('#response')
                                        .addClass('ui-state-error ui-corner-all')
                                        .html(html);       
                                        
                                    $('#dialling-codes')
                                        .addClass('ui-state-error');
                            }
                        else if(parsed[0] == "empty_type")
                            {
                                    html += "<p><span class='highlight-icon ui-icon ui-icon-alert'></span>";                                
                                    html += parsed[1] +"</p>";
                                
                                    $('#response')
                                        .addClass('ui-state-error ui-corner-all')
                                        .html(html);       
                                        
                                    $('#type')
                                        .addClass('ui-state-error');
                            }
							
                        else if(parsed[0] == "letters_in_dialcodes")
                            {
                                    html += "<p><span class='highlight-icon ui-icon ui-icon-alert'></span>";
                                    html += parsed[1] +"</p>";

                                    $('#response')
                                        .addClass('ui-state-error ui-corner-all')
                                        .html(html);

                                    $('#dialling-codes')
                                        .addClass('ui-state-error');
                            }
                        else if(parsed[0] == "duplicate_entries_in_dialcodes")
                            {
                                    html += "<p><span class='highlight-icon ui-icon ui-icon-alert'></span>";
                                    html += parsed[1] +"</p>";

                                    $('#response')
                                        .addClass('ui-state-error ui-corner-all')
                                        .html(html);

                                    $('#dialling-codes')
                                        .addClass('ui-state-error');
                            }

                        else if(parsed[0] == "dialcodes_from_different_countries")
                            {
                                    html += "<p><span class='highlight-icon ui-icon ui-icon-alert'></span>";
                                    html += parsed[1] +"</p>";

                                    $('#response')
                                        .addClass('ui-state-error ui-corner-all')
                                        .html(html);

                                    $('#dialling-codes')
                                        .addClass('ui-state-error');
                            }
                        else if(parsed[0] == "dialcodes_do_not_have_country")
                            {
                                    html += "<p><span class='highlight-icon ui-icon ui-icon-alert'></span>";
                                    html += parsed[1] +"</p>";

                                    $('#response')
                                        .addClass('ui-state-error ui-corner-all')
                                        .html(html);

                                    $('#dialling-codes')
                                        .addClass('ui-state-error');
                            }
							
                        else if(parsed[0] == "routes_already_exist")
                            {
                                    html += "<p><span class='highlight-icon ui-icon ui-icon-alert'></span>";
                                    html += parsed[1] +"</p>";

                                    $('#response')
                                        .addClass('ui-state-error ui-corner-all')
                                        .html(html);

									$('#route-name')
										.addClass('ui-state-error');
										
                                    $('#dialling-codes')
                                        .addClass('ui-state-error');
										
                            }

                        else if(parsed[0] == "prefixes_overlap")
                            {
                                    html += "<p><span class='highlight-icon ui-icon ui-icon-alert'></span>";
                                    html += parsed[1] +"</p>";

                                    $('#response')
                                        .addClass('ui-state-error ui-corner-all')
                                        .html(html);

                                    $('#dialling-codes')
                                        .addClass('ui-state-error');

                                    $('#date-from')
                                        .addClass('ui-state-error');
										
                            }
                        else if(parsed[0] == "empty_start_date")
                            {
                                    html += "<p><span class='highlight-icon ui-icon ui-icon-alert'></span>";
                                    html += parsed[1] +"</p>";

                                    $('#response')
                                        .addClass('ui-state-error ui-corner-all')
                                        .html(html);

                                    $('#date-from')
                                        .addClass('ui-state-error');
										
                            }							
                        else if(parsed[0] == "start_date_too_low")
                            {
                                    html += "<p><span class='highlight-icon ui-icon ui-icon-alert'></span>";
                                    html += parsed[1] +"</p>";

                                    $('#response')
                                        .addClass('ui-state-error ui-corner-all')
                                        .html(html);

                                    $('#date-from')
                                        .addClass('ui-state-error');
										
                            }														
                        else if(parsed[0] == "pending_close_routes")
                            {
                                    html += "<p><span class='highlight-icon ui-icon ui-icon-alert'></span>";
                                    html += parsed[1] +"</p>";

                                    $('#response')
                                        .addClass('ui-state-error ui-corner-all')
                                        .html(html);

                                    $('#dialling-codes')
                                        .addClass('ui-state-error');

                                    $('#date-from')
                                        .addClass('ui-state-error');
										
                            }
                        else if(parsed[0] == "pending_open_routes")
                            {
                                    html += "<p><span class='highlight-icon ui-icon ui-icon-alert'></span>";
                                    html += parsed[1] +"</p>";

                                    $('#response')
                                        .addClass('ui-state-error ui-corner-all')
                                        .html(html);

                                    $('#dialling-codes')
                                        .addClass('ui-state-error');

                                    $('#date-from')
                                        .addClass('ui-state-error');
										
                            }
							
							
						else if	(parsed[0] == "pending_confirmation")
							{
								$('#routes').html(parsed[2]);
								$( "#confirmation-box" ).dialog( "open" );								
							}
							
                         else
                             {
                                 alert(response);
                             }

			}
		  });

}

function close_route()
{
        ajaxURL = AJAX_URL + '/close_route/' + numberingPlan + '/';
            
        //alert(ajaxURL);

	$.ajax({
			type: "POST",
			url: ajaxURL,
			data: $('#new-route').serialize(),
			async: true,
			success: function(response){

                        reset_error_state();
			
                        var parsed = response.split('|');
                        
                        var html = '';
                        
                        if(parsed[0] == "true")
                            {
                                    html += "<p><span class='highlight-icon ui-icon ui-icon-info'></span>";                                
                                    html += parsed[1] +"</p>";
                                
                                    $('#response')
                                        .addClass('ui-state-highlight ui-corner-all')
                                        .html(html);                                
                            }

                        else if(parsed[0] == "empty_end_date")
                            {
                                    html += "<p><span class='highlight-icon ui-icon ui-icon-alert'></span>";
                                    html += parsed[1] +"</p>";

                                    $('#response')
                                        .addClass('ui-state-error ui-corner-all')
                                        .html(html);

                                    $('#date-from')
                                        .addClass('ui-state-error');
										
                            }							
                        else if(parsed[0] == "end_date_too_low")
                            {
                                    html += "<p><span class='highlight-icon ui-icon ui-icon-alert'></span>";
                                    html += parsed[1] +"</p>";

                                    $('#response')
                                        .addClass('ui-state-error ui-corner-all')
                                        .html(html);

                                    $('#date-from')
                                        .addClass('ui-state-error');
										
                            }																												
						else if	(parsed[0] == "pending_confirmation")
							{
								$('#routes').html(parsed[2]);
								$( "#close-confirmation-box" ).dialog( "open" );								
							}
							
                         else
                             {
                                 alert(response);
                             }

			}
		  });

}

function reset_error_state()
{
            // reset error states
            $('#response')
                      .removeClass('ui-state-error ui-corner-all')
                      .html('&nbsp;');

            $('#route-name')
                      .removeClass('ui-state-error');
            $('#dialling-codes')
                      .removeClass('ui-state-error');
            $('#date-from')
                      .removeClass('ui-state-error');					 
            $('#type')
                      .removeClass('ui-state-error');
					  
					  
}
